package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.ServiceItem;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.mapper.StatementItemMapper;
import cn.wolfcode.car.business.query.ServiceItemQuery;
import cn.wolfcode.car.business.query.StatementItemQuery;
import cn.wolfcode.car.business.service.IServiceItemService;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
@Transactional
public class StatementItemServiceImpl implements IStatementItemService {

    @Autowired
    private StatementItemMapper statementItemMapper;

    //结算单对象
    @Autowired
    private IStatementService statementService;


    public TablePageInfo<StatementItem> query(StatementItemQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<StatementItem>(statementItemMapper.selectForList(qo));
    }


    @Override
    public void save(StatementItem statementItem) {
        statementItemMapper.insert(statementItem);
    }

    @Override
    public StatementItem get(Long id) {
        return statementItemMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(StatementItem statementItem) {
        statementItemMapper.updateByPrimaryKey(statementItem);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            statementItemMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<StatementItem> list() {
        return statementItemMapper.selectAll();
    }

    @Override
    public List<StatementItem> itemDetail(Long statementId) {
        statementItemMapper.itemDetail(statementId);
        return null;
    }

    @Override
    public void saveItems(List<StatementItem> items) {
        //在处理方法接收 JSON格式的数据时，需要通过注解RequestBody 接收，否则无法接收
        //思考：可能存在为空的请求，所以我们需要判断,并重新设置数据进数据库，删除明细表中对应的数据


        if (items != null){
            //获取最后一个，折扣价数据
            StatementItem si = items.remove(items.size() - 1);
            statementItemMapper.deleteByStatementId(si.getStatementId());

            //总数
            BigDecimal totalCount = new BigDecimal("0");
            //总金额
            BigDecimal totalAmount = new BigDecimal("0.00");

            //如果长度大于0，说明有服务单项，那么就需要计算结果
            if (items.size() > 0){
                //结算单明细
                for (StatementItem item : items) {
                    //总数
                    totalCount = totalCount.add(new BigDecimal(item.getItemQuantity()));
                    //总金额'
                    //.multiply 表示BigDecimal的乘法 总金额= 单价乘以数量
                    totalAmount = totalAmount.add(item.getItemPrice()
                            .multiply(new BigDecimal(item.getItemQuantity())));

                    //执行插入操作,插入数据到明细编辑表
                    statementItemMapper.insert(item);
                }

                //修改结算表中的数据 根据结算单id，需要修改的是，结算单id, 优惠价，         总金额，    个数
                statementService.updateAmount(si.getStatementId(),si.getItemPrice(),totalAmount,totalCount);

            }else {
                //清除总金额中的数据，包括折扣
                statementService.updateAmount(si.getStatementId(),new BigDecimal("0.00"),totalAmount,totalCount);
            }

        }

    }


}
